﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameDataTempshitEditor.Widgets
{
    public abstract class PropertyItemAction
    {
        public readonly PropertyItemBase Item;
        public readonly int ImageIndex;
        public readonly string Name;
        public Cursor MouseCursor;

        public PropertyItemAction(PropertyItemBase item, int imageIndex = -1 )
        {
            Item = item;
            ImageIndex = imageIndex;
            Name = "";
            MouseCursor = Cursors.Arrow;
        }

        public PropertyItemAction(PropertyItemBase item, string name)
        {
            Item = item;
            Name = name;
            ImageIndex = -1;
            MouseCursor = Cursors.Arrow;
        }

        public abstract bool Perform(ref string errorMessage);
    }

    public class PropertyItemAction_ExpandButton : PropertyItemAction
    {
        public PropertyItemAction_ExpandButton(PropertyItemBase item)
            : base(item)
        {
            MouseCursor = Cursors.Hand;
        }

        public override bool Perform(ref string errorMessage)
        {
            if (Item.CanExpand)
            {
                if (!Item.IsExpanded)
                {
                    if (!Item.Expand())
                    {
                        errorMessage = "Unable to expand item";
                        return false;
                    }
                }
                else
                {
                    if (!Item.Collapse())
                    {
                        errorMessage = "Unable to collapse item";
                        return false;
                    }
                }

                return true;
            }

            // invalid action
            return false;
        }
    }

    public class PropertyItemAction_Select : PropertyItemAction
    {
        public PropertyItemAction_Select(PropertyItemBase item)
            : base(item)
        {
        }

        public override bool Perform(ref string errorMessage)
        {
            return true;
        }
    }

    public class PropertyItemAction_Button : PropertyItemAction
    {
        //---------------------

        public delegate bool ActionClickHandler(PropertyItemBase item, ref string errorMessage);
        private event ActionClickHandler OnClick;

        //---------------------

        public PropertyItemAction_Button(PropertyItemBase item, int imageIndex, ActionClickHandler func)
            : base(item, imageIndex)
        {
            OnClick += func;
        }

        public PropertyItemAction_Button(PropertyItemBase item, string name, ActionClickHandler func)
            : base(item, name)
        {
            OnClick += func;
        }

        public override bool Perform(ref string errorMessage)
        {
            if (OnClick != null)
                return OnClick(Item, ref errorMessage);

            return false;
        }
 
    }

}
